package product.controller;


import com.feign.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import product.domain.Category;
import product.service.CategoryService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品三级分类(Category)表控制层
 *
 * @author panghu
 * @since 2023-03-04 21:09:17
 */
@RestController
@RequestMapping("/product/category/")
public class CategoryController {


    @Autowired
    private CategoryService categoryService;

    /**
     * 查询树形排序好好的节点
     *
     * @return
     */
    @GetMapping("list")
    public R getCategoryList() {
        List<Category> categoryList = categoryService.findAllCategoryList();
        return R.ok().put("categoryList", categoryList);
    }


    /**
     * 删除节点
     *
     * @param ids
     * @return
     */
    @PostMapping("remove")
    public R removeCategory(@RequestBody Long[] ids) {
        boolean result = categoryService.removeCategoryByIds(List.of(ids));
        return result ? R.ok("删除节点成功") : R.error("删除节点失败");
    }


    /**
     * 增加节点
     *
     * @param category
     * @return
     */
    @PostMapping("save")
    public R save(@RequestBody Category category) {
        boolean result = categoryService.save(category);
        return result ? R.ok("增加节点成功") : R.error("增加节点失败");
    }

    /**
     * 修改节点
     *
     * @param category
     * @return
     */
    @PutMapping("edit")
    public R edit(@RequestBody Category category) {
        boolean result = categoryService.updateCascade(category);
        return result ? R.ok("修改成功") : R.error("修改失败");
    }


    /**
     * 获取单个节点的数据
     *
     * @param catId
     * @return
     */
    @GetMapping("info/{catId}")
    public R info(@PathVariable Long catId) {
        Category category = categoryService.getById(catId);
        return R.ok().put("data", category);
    }


    @PostMapping("update")
    public R updateNodes(@RequestBody List<Category> categoryList) {
        boolean result = categoryService.updateBatchById(categoryList);
        return result ? R.ok("拖拽节点更新成功") : R.error("拖拽节点更新失败");
    }
}

